package fr.azzale.casu.views;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import fr.azzale.casu.R;
import fr.azzale.casu.metier.Line;

public class LineView extends LinearLayout {

	private TextView text;
	private TextView date;
	private TextView nom;

	public LineView(Context context) {
		super(context);
		LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = li.inflate(R.layout.lineview, null);
		text = (TextView)v.findViewById(R.id.text);
		date = (TextView)v.findViewById(R.id.date);
		nom = (TextView)v.findViewById(R.id.nom);
		addView(v);
	}

	public void display(Line item) {
		text.setText(Html.fromHtml(item.getText(), new ImageGetter() {                 
		    @Override
		    public Drawable getDrawable(String img) {
		    	Drawable d = null;
				Resources r = getResources();
				if(img.endsWith("afro.gif/"))
					d = r.getDrawable(R.drawable.afro);
				else if(img.endsWith("angel.gif/"))
					d = r.getDrawable(R.drawable.angel);
				else if(img.endsWith("angry.gif/"))
					d = r.getDrawable(R.drawable.angry);
				else if(img.endsWith("azn.gif/"))
					d = r.getDrawable(R.drawable.azn);
				else if(img.endsWith("cheezy.gif/"))
					d = r.getDrawable(R.drawable.cheesy);
				else if(img.endsWith("cool.gif/"))
					d = r.getDrawable(R.drawable.cool);
				else if(img.endsWith("cry.gif/"))
					d = r.getDrawable(R.drawable.cry);
				else if(img.endsWith("embarrassed.gif/"))
					d = r.getDrawable(R.drawable.embarrassed);
				else if(img.endsWith("evil.gif/"))
					d = r.getDrawable(R.drawable.evil);
				else if(img.endsWith("grin.gif/"))
					d = r.getDrawable(R.drawable.grin);
				else if(img.endsWith("huh.gif/"))
					d = r.getDrawable(R.drawable.huh);
				else if(img.endsWith("kiss.gif/"))
					d = r.getDrawable(R.drawable.kiss);
				else if(img.endsWith("laugh.gif/"))
					d = r.getDrawable(R.drawable.laugh);
				else if(img.endsWith("lipsrsealed.gif/"))
					d = r.getDrawable(R.drawable.lipsrsealed);
				else if(img.endsWith("mu_lol.gif/"))
					d = r.getDrawable(R.drawable.mu_lol);
				else if(img.endsWith("police.gif/"))
					d = r.getDrawable(R.drawable.police);
				else if(img.endsWith("puck.gif/"))
					d = r.getDrawable(R.drawable.puck);
				else if(img.endsWith("rolleyes.gif/"))
					d = r.getDrawable(R.drawable.rolleyes);
				else if(img.endsWith("sad.gif/"))
					d = r.getDrawable(R.drawable.sad);
				else if(img.endsWith("shocked.gif/"))
					d = r.getDrawable(R.drawable.shocked);
				else if(img.endsWith("sleepy.gif/"))
					d = r.getDrawable(R.drawable.sleepy);
				else if(img.endsWith("smiley.gif/"))
					d = r.getDrawable(R.drawable.smiley);
				else if(img.endsWith("tongue.gif/"))
					d = r.getDrawable(R.drawable.tongue);
				else if(img.endsWith("undecided.gif/"))
					d = r.getDrawable(R.drawable.undecided);
				else if(img.endsWith("wink.gif/"))
					d = r.getDrawable(R.drawable.wink);
				else if(img.endsWith("cheezy.gif/"))
					d = r.getDrawable(R.drawable.wink);
				if(d!=null)
					d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

		        return d;
		    }
		}, null));
		date.setText(item.getFormattedDate());
		nom.setText(item.getOwner().getNom());
		nom.setTextColor(item.getOwner().getColor());
		if(item.getOwner().getImage()!=null)
			nom.setCompoundDrawablesWithIntrinsicBounds(item.getOwner().getImage(), null, null, null);
	}
}
